<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/7
 * Time: 9:29
 */

namespace App\Models;

class Permissions extends BaseModel
{
    /**
     * 与模型关联的数据表
     *
     * @var string
     */
    protected $table = 'permissions';

    /**
     * 主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * @var array
     */
    protected $appends = ['children'];

    /**
     * @return bool
     */
    public function getChildrenAttribute()
    {
        if ($has= static::children()->first()){
            return true;
        }
        return false;
    }

    /**
     * @param $value
     * @return mixed
     */
    public function setChildrenAttribute($value)
    {
        $this->attributes['children'] = $value;
    }

    /**
     * 获取下一级权限数据
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function children(){
        return $this->hasMany(self::class, 'parent_id');
    }

    /**
     * 父级数据
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function parent(){
        return $this->hasOne(self::class, 'id', 'parent_id');
    }
}